package com.findMoneyGame.pages.shop {

	import flash.events.MouseEvent;
	import flash.events.Event;

	import com.findMoneyGame.pages.DialogPage;
	import com.findMoneyGame.pages.ClosablePage;
	import com.findMoneyGame.models.User;
	import com.findMoneyGame.pages.shop.ImprovementsConfig;

	public class PurchasingsPage extends DialogPage {

		public static const NEED_MORE_MONEY : String = "needMoreMoney";


		private function get user () : User { return User.getInst(); }


		public function PurchasingsPage () {
			gotoAndStop('normal');
			closeButton.addEventListener(MouseEvent.CLICK, closeBtnEventHandler);
			buyBonusTimeButton.addEventListener(MouseEvent.CLICK, buyBonusTimeButtonClick);
			buyBonusLoupeButton.addEventListener(MouseEvent.CLICK, buyBonusLoupeButtonClick);
			buyBonusHintButton.addEventListener(MouseEvent.CLICK, buyBonusHintButtonClick);
		}
		private function buyBonusTimeButtonClick (e : Event) {
			if ((user.timeBonusIndex + 1) >= ImprovementsConfig.timeBonusesPrices.length) {
				return;
			}
			var price = ImprovementsConfig.timeBonusesPrices[user.timeBonusIndex + 1];
			if (user.moneyCount >= price) {
				var yes = function (e : Event) {
					goFromConfirmToNormalFrame(yes, no);
					user.takeMoney(price);
					user.setNextTimeBonusIndex();
				}
				var no = function (e : Event) { goFromConfirmToNormalFrame(yes, no); }
				goFromNormalToConfirmFrame(yes, no);
			} else {
				goFromNormalToBuyMoneyFrame();
			}
		}
		private function buyBonusLoupeButtonClick (e : Event) {
			if ((user.loupeBonusIndex + 1) >= ImprovementsConfig.loupeBonusesPrices.length) {
				return;
			}
			var price = ImprovementsConfig.loupeBonusesPrices[user.loupeBonusIndex + 1];
			if (user.moneyCount >= price) {
				var yes = function (e : Event) {
					goFromConfirmToNormalFrame(yes, no);
					user.takeMoney(price);
					user.setNextLoupeBonusIndex();
				}
				var no = function (e : Event) { goFromConfirmToNormalFrame(yes, no); }
				goFromNormalToConfirmFrame(yes, no);
			} else {
				goFromNormalToBuyMoneyFrame();
			}
		}
		private function buyBonusHintButtonClick (e : Event) {
			if ((user.hintBonusIndex + 1) >= ImprovementsConfig.hintBonusesPrices.length) {
				return;
			}
			var price = ImprovementsConfig.hintBonusesPrices[user.hintBonusIndex + 1];
			if (user.moneyCount >= price) {
				var yes = function (e : Event) {
					goFromConfirmToNormalFrame(yes, no);
					user.takeMoney(price);
					user.setNextHintBonusIndex();
				}
				var no = function (e : Event) { goFromConfirmToNormalFrame(yes, no); }
				goFromNormalToConfirmFrame(yes, no);
			} else {
				goFromNormalToBuyMoneyFrame();
			}
		}

		private function goFromNormalToConfirmFrame (yes : Function, no : Function) {
			gotoAndStop('confirm');
			confirmDialog.addEventListener(PurchasingConfirmDialog.YES, yes);
			confirmDialog.addEventListener(PurchasingConfirmDialog.NO, no);
		}
		private function goFromConfirmToNormalFrame (yes : Function, no : Function) {
			confirmDialog.removeEventListener(PurchasingConfirmDialog.YES, yes);
			confirmDialog.removeEventListener(PurchasingConfirmDialog.NO, no);
			gotoAndStop('normal');
		}
		private function goFromNormalToBuyMoneyFrame () {
			gotoAndStop('buyMoney');
			buyMoneyDialog.addEventListener(ClosablePage.CLOSE_EVENT, closeBuyMoneyDialog);
		}
		private function goFromBuyMoneyToNormalFrame () {
			buyMoneyDialog.removeEventListener(ClosablePage.CLOSE_EVENT, closeBuyMoneyDialog);
			gotoAndStop('normal');
		}

		private function closeBuyMoneyDialog (e : Event) { goFromBuyMoneyToNormalFrame(); }

	}
}